//package top.qitun.service;
//
//import org.apache.commons.codec.digest.DigestUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.github.prontera.Shift;
//import com.github.prontera.persistence.CrudMapper;
//import com.github.prontera.service.CrudServiceImpl;
//import com.github.prontera.util.OrikaMapper;
//import com.google.common.base.Preconditions;
//import com.google.common.html.HtmlEscapers;
//
//import top.qitun.bizz.account.tuser.entity.TUser;
//import top.qitun.bizz.account.tuser.service.TUserService;
//import top.qitun.bizz.auth.dto.AuthenticationDTO;
//import top.qitun.controller.StatusCode;
//import top.qitun.domain.User;
//import top.qitun.model.request.RegisterRequest;
//import top.qitun.model.response.RegisterResponse;
//
///**
// * @author Zhao Junjian
// */
//@Service
//public class UserService extends CrudServiceImpl<User> {
//    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
//
//    @Autowired
//    private TUserService userServiceImpl;
//
//    @Autowired
//    public UserService(CrudMapper<User> mapper) {
//        super(mapper);
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public RegisterResponse register(RegisterRequest request) throws Exception {
//        Preconditions.checkNotNull(request);
//        final TUser dbUser = find(request.getMobile());
//        if (dbUser != null) {
//            Shift.fatal(StatusCode.USER_EXISTS);
//        }
//        // 重新计算密码
//        final TUser transientUser = OrikaMapper.map(request, TUser.class);
////        final String salt = generateRandomPasswordSalt();
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        final String loginPassword = passwordEncoder.encode(dbUser.getLoginPwd());
////        final String loginPassword = digestWithSalt(transientUser.getLoginPwd(), salt);
////        transientUser.setPwdSalt(salt);
//        dbUser.setLoginPwd(loginPassword);
//        // 混合盐后入库
////        persistNonNullProperties(dbUser);
//        userServiceImpl.add(transientUser);
//        return OrikaMapper.map(transientUser, RegisterResponse.class);
//    }
//
//    public TUser login(AuthenticationDTO request) {
//        Preconditions.checkNotNull(request);
//        final TUser user = find(request.getMobiel());
//        if (user == null) {
//            Shift.fatal(StatusCode.USER_NOT_EXISTS);
//        }
//        // 登录用户的密码摘要
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
////        final String requestLoginPWd = digestWithSalt(request.getLoginPwd(), user.getPwdSalt());
////        if (!Objects.equal(requestLoginPWd, user.getLoginPwd())) {
//        if (passwordEncoder.matches(request.getLoginPwd(), user.getLoginPwd())) {
//            Shift.fatal(StatusCode.INVALID_CREDENTIAL);
//        }
//        return user;
//    }
//
//    public TUser find(String mobile) {
//        Preconditions.checkNotNull(mobile);
//        TUser result = null;
//        if (!mobile.isEmpty()) {
//            final String escapeMobile = HtmlEscapers.htmlEscaper().escape(mobile);
//            result = userServiceImpl.selectByMobile(escapeMobile);
//        }
//        return result;
//    }
//
////    public List<User> findAll(int offset, int limited) {
////        Preconditions.checkArgument(offset > -1);
////        Preconditions.checkArgument(limited > -1);
////        return mapper.selectAll(offset, limited);
////    }
//
//    private String digestWithSalt(String content, String key) {
//        String result = content;
//        for (int i = 0; i < 5; i++) {
//            result = DigestUtils.sha256Hex(result + key);
//        }
//        return result;
//    }
//
//    private String generateRandomPasswordSalt() {
//        return DigestUtils.sha256Hex(String.valueOf(System.nanoTime()));
//    }
//
//    
//    public static void main(String[] args) {
//    	BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        final String loginPassword = passwordEncoder.encode("123456");
//        boolean result = passwordEncoder.matches("123456", loginPassword);
//        System.out.println(result);
//	}
//}
